var io = require('socket.io-client');

const hostname = "127.0.0.1";

const socket_protocol = "ws";
const socket_port = "9090";


function randomString(e) {
    e = e || 32;
    var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
        a = t.length,
        n = "";
    for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
}

async function init(indexId) {
    // console.log("initing...")
    let userid = randomString();

    let url = `${socket_protocol}://${hostname}:${socket_port}/im/user?id=${indexId}-${userid}`;
    let socket = io.connect(url, {
        transports: ['websocket']
    });

    console.log(indexId + "-initing success.")

    setTimeout(() => {

        socket.on("message", function (data) {
            console.log(indexId + "-[message]", data);
        })
        // setInterval(() => {
        //     if (socket) {
        //         socket.emit("message", {
        //             id: indexId,
        //             msgType: "text",
        //             message: randomString(),
        //         }, () => {
        //             console.log(indexId + "-send msg success.")
        //         });
        //     }
        // }, 1000);
    }, 1000);

    console.log("finish.")
};



for (let index = 0; index < 10000; index++) {

    init(index + 1);
}

console.log("finished.")